package com.good.child.ui.adapter.constants;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.good.child.R;
import com.good.child.bean.constants.ConstantsBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/8/4
 * by Mr.Zhu
 * Description: 联系人item
 */
public class ConstantsListAdapter extends RecyclerView.Adapter<ConstantsListAdapter.MyViewHolder> {

    private Context context;
    private List<ConstantsBean> dataList;
    private onItemClick onItemClick;

    public ConstantsListAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ConstantsBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public ConstantsListAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        View inflater = LayoutInflater.from(context).inflate(R.layout.item_constants, parent, false);
        ConstantsListAdapter.MyViewHolder myViewHolder = new ConstantsListAdapter.MyViewHolder(inflater);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ConstantsListAdapter.MyViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        ConstantsBean bean = dataList.get(position);

        holder.nameTv.setText(bean.getName());
        holder.phone.setText(TextUtils.isEmpty(bean.getTelephone()) ? "" : bean.getTelephone());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onClickView(bean.getTelephone());
                }
            }
        });

        Glide.with(context).load(bean.getAvatar()).placeholder(context.getResources().getDrawable(R.mipmap.default_head)).into(holder.profileImage);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    //内部类，绑定控件
    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.constants_name)
        TextView nameTv;
        @BindView(R.id.constants_phone)
        TextView phone;
        @BindView(R.id.profile_image)
        ImageView profileImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onItemClick {
        void onClickView(String name);
    }
}

